#ifndef __PMD_H__
#define __PMD_H__
/////////////////
//include
/////////////////
#include<iostream>
#include<fstream>
#include<hash_map>
#include<map>
#include"Vector2D.h"
#include"Model.h"
#include"Device.h"
#include"DeviceContext.h"
#include"Matrix.h"
/////////////////
//const
/////////////////

/////////////////
//class
/////////////////
class VMD;
class PMD : public Model
{
public:
	struct Header
	{
		char magic[3];
		float vertion;
		char model_name[20];
		char comment[256];
	};
	struct VertexElement
	{
		Vector3D pos;
		Vector3D normal;
		Vector2D uv;
		WORD bone_num[2];
		BYTE bone_weight;
		BYTE edgi_flag;
	};
	struct Vertex
	{
		DWORD vertex_count;
		Array<VertexElement> element;
	};
	struct FaceVertex
	{
		DWORD				vertex_count;
		Array<WORD>	face_vertex;
	};
	struct MaterialElement
	{
		Vector3D diffuse;
		float alpha;
		float specularity;
		Vector3D specula;
		Vector3D ambient;
		BYTE toon_index;
		BYTE edge_flag;
		DWORD face_vert_count;
		char texture_file_name[20];
	};
	struct Material
	{
		DWORD element_num;
		Array<MaterialElement> material;
	};
	struct BoneElement
	{
		char bone_name[20];
		WORD parent_bone_index;
		WORD tail_pos_bone_index;
		BYTE bone_index;
		WORD ik_parent_index;
		Vector3D bone_head_pos;
	};
	struct Bone
	{
		WORD bone_num;
		Array<BoneElement> bone;
	};
	struct BoneInfo
	{
		std::string name;
		Matrix mat;
		BoneElement bone_element;
	};
	struct VertexInfo
	{
		Vector3D pos;
		Vector3D normal;
		Vector2D uv;
		WORD bone_num[2];
		BYTE bone_weight;
		BYTE dummy[3];
	};
	struct MaterialInfo
	{
		Vector3D diffuse;
		float alpha;
		float specularity;
		Vector3D specula;
		Vector3D ambient;
		float dummy;
	};
	PMD(std::string address = "",std::string name = "");
	~PMD();
	void	CreateBuffer(Device* device);
	void	CreateShader(Device* device);
	void	Draw(DeviceContext* device_context);
	void	SetAnimation(const std::string& motion_file_name);
	void	SetVmd(VMD* vmd);
	void	Play(int anime_index,int anime_count,bool is_loop);
	TriCollInfo	CollisionLine(int anime_index,int anime_count,Vector3D start,Vector3D end, Vector3D pos, Matrix rot_mat,float size);
	Matrix	Propagation(Array<BoneInfo>& bone_info, Matrix& mat,int next_bone);

	int		GetMaxAnimationCount(int anim_index);
	Polygons* GetPolygons();
	Polygons* GetPolygons(int anim_index,int anime_count);
	Vector3D GetBonePos(const std::string& bone_name,Vector3D pos,const Matrix& rot,float size);
	bool IsInCamera(Camera* camera);
private:
	Header			header;
	Vertex			vertex;
	FaceVertex		face_vertex;
	Material		material;
	Array<ID3D11ShaderResourceView*> texture;
	std::string				address;
	Array<VertexInfo>		vertex_info;
	Array<MaterialInfo>		material_info;
	std::map<std::string,VMD*>				motions_map;
	Array<VMD*>				motions;
	Array<BoneElement>		bone;
	Array<BoneInfo>			bone_info;
	Array<Matrix>			rot_matrix;
	Array<Matrix>			pos_matrix;
	Array<Matrix>			scale_matrix;
	//Array<BoneInfo>			tmp_bone;
	Polygons poly;
	float polygon_size;
	Array<BoneInfo> tmp_bone;
};
#endif __PMD_H__
